package be.ibridge.kettle.UpdateRRD;

import java.util.List;
import java.util.Map;

import org.eclipse.swt.widgets.Shell;
import org.pentaho.di.core.CheckResult;
import org.pentaho.di.core.CheckResultInterface;
import org.pentaho.di.core.Counter;
import org.pentaho.di.core.database.DatabaseMeta;
import org.pentaho.di.core.exception.KettleDatabaseException;
import org.pentaho.di.core.exception.KettleException;
import org.pentaho.di.core.exception.KettleXMLException;
import org.pentaho.di.core.row.RowMetaInterface;
import org.pentaho.di.core.variables.VariableSpace;
import org.pentaho.di.core.xml.XMLHandler;
import org.pentaho.di.repository.Repository;
import org.pentaho.di.trans.Trans;
import org.pentaho.di.trans.TransMeta;
import org.pentaho.di.trans.step.BaseStepMeta;
import org.pentaho.di.trans.step.StepDataInterface;
import org.pentaho.di.trans.step.StepDialogInterface;
import org.pentaho.di.trans.step.StepInterface;
import org.pentaho.di.trans.step.StepMeta;
import org.pentaho.di.trans.step.StepMetaInterface;
import org.w3c.dom.Node;

/*
 * Created on 02-jun-2003
 *
 */

public class UpdateRRDPluginMeta extends BaseStepMeta implements StepMetaInterface
{
	//private ValueMetaAndData value;
	private String RRDFilename;
	private String RRDDatasource;
	private String RRDType;
	private String RRDHeartBeat;
	private String RRDStartTime;
	private String RRDMini;
	private String RRDMaxi;
	
	private String RRDXFile;
	private String RRDFType;
	private String RRDNBSteps;
	private String RRDNBRows;
	
	private String RRDArchiveItems;
	
	
	public UpdateRRDPluginMeta()
	{
		super(); // allocate BaseStepInfo
	}
	// Filename
	public String getRRDFilename() {
		return RRDFilename;
	}
	public void setRRDFilename(String RRDFilename) {
		this.RRDFilename = RRDFilename;
	}
	// DataSource
	public String getRRDDatasource() {
		return RRDDatasource;
	}
	public void setRRDDatasource(String RRDDatasource) {
		this.RRDDatasource = RRDDatasource;
	}
	// Type
	public String getRRDType() {
		return RRDType;
	}
	public void setRRDType(String RRDType) {
		this.RRDType = RRDType;
	}
	// HeartBeat
	public String getRRDHeartBeat() {
		return RRDHeartBeat;
	}
	public void setRRDHeartBeat(String RRDHeartBeat) {
		this.RRDHeartBeat = RRDHeartBeat;
	}
	// StartTime
	public String getRRDStartTime() {
		return RRDStartTime;
	}
	public void setRRDStartTime(String RRDStartTime) {
		this.RRDStartTime = RRDStartTime;
	}
	// Mini
	public String getRRDMini() {
		return RRDMini;
	}
	public void setRRDMini(String RRDMini) {
		this.RRDMini = RRDMini;
	}
	// Maxi
	public String getRRDMaxi() {
		return RRDMaxi;
	}
	public void setRRDMaxi(String RRDMaxi) {
		this.RRDMaxi = RRDMaxi;
	}
	//FType
	public void setRRDFType(String RRDFType) {
		this.RRDFType = RRDFType;
	}
	public String getRRDFType() {
		return RRDFType;
	}
	// NB Steps
	public String getRRDNBSteps() {
		return RRDNBSteps;
	}
	public void setRRDNBSteps(String RRDNBSteps) {
		this.RRDNBSteps = RRDNBSteps;
	}
	// NB Rows
	public String getRRDNBRows() {
		return RRDNBRows;
	}
	public void setRRDNBRows(String RRDNBRows) {
		this.RRDNBRows = RRDNBRows;
	}
	//XFile
	public String getRRDXFile() {
		return RRDXFile;
	}
	public void setRRDXFile(String RRDXFile) {
		this.RRDXFile = RRDXFile;
	}
	//Archive table
	public void setRRDArchives(String RRDArchiveItems) {
		this.RRDArchiveItems = RRDArchiveItems;
	}
	public String getRRDArchives() {
		return RRDArchiveItems;			
	}



	
	
	/**
	 * @return Returns the value.
	 */
	/*
	public ValueMetaAndData getValue()
	{
		return value;
	}
	*/
	/**
	 * @param value The value to set.
	 */
	/*
	public void setValue(ValueMetaAndData value)
	{
		this.value = value;
	}
	*/
	/////////////////////////////////////////////////////////////
	public String getXML()
	{
        final StringBuilder retval = new StringBuilder(1000);

        retval.append("   ").append(XMLHandler.addTagValue("RRDFilename", RRDFilename)); //$NON-NLS-1$ //$NON-NLS-2$
        retval.append("   ").append(XMLHandler.addTagValue("RRDDatasource", RRDDatasource)); //$NON-NLS-1$ //$NON-NLS-2$
        retval.append("   ").append(XMLHandler.addTagValue("RRDType", RRDType)); //$NON-NLS-1$ //$NON-NLS-2$
        retval.append("   ").append(XMLHandler.addTagValue("RRDHeartBeat", RRDHeartBeat)); //$NON-NLS-1$ //$NON-NLS-2$
        retval.append("   ").append(XMLHandler.addTagValue("RRDStartTime", RRDStartTime)); //$NON-NLS-1$ //$NON-NLS-2$
        retval.append("   ").append(XMLHandler.addTagValue("RRDMini", RRDMini)); //$NON-NLS-1$ //$NON-NLS-2$
        retval.append("   ").append(XMLHandler.addTagValue("RRDMaxi", RRDMaxi)); //$NON-NLS-1$ //$NON-NLS-2$
        retval.append("   ").append(XMLHandler.addTagValue("RRDArchiveItems", RRDArchiveItems)); //$NON-NLS-1$ //$NON-NLS-2$
        
        return retval.toString();
	}

	public void getFields(RowMetaInterface r, String origin, RowMetaInterface[] info, StepMeta nextStep, VariableSpace space)
	{
		/*if (value!=null)
		{
			ValueMetaInterface v = value.getValueMeta();
			v.setOrigin(origin);
			
			r.addValueMeta( v );
			*/
		/*
		ValueMetaInterface v = new ValueMeta("RRDFilename", ValueMeta.TYPE_STRING, 44, 0);
		v.setOrigin(origin);
		r.addValueMeta(v);
		
		v = new ValueMeta("RRDDatasource", ValueMeta.TYPE_STRING, 44, 0);
		v.setOrigin(origin);
		r.addValueMeta(v);
		
		v = new ValueMeta("RRDType", ValueMeta.TYPE_STRING, 44, 0);
		v.setOrigin(origin);
		r.addValueMeta(v);
		
		v = new ValueMeta("RRDHeartBeat", ValueMeta.TYPE_STRING, 44, 0);
		v.setOrigin(origin);
		r.addValueMeta(v);
		
		v = new ValueMeta("RRDStartTime", ValueMeta.TYPE_STRING, 44, 0);
		v.setOrigin(origin);
		r.addValueMeta(v);
		
		v = new ValueMeta("RRDMini", ValueMeta.TYPE_STRING, 44, 0);
		v.setOrigin(origin);
		r.addValueMeta(v);
		
		v = new ValueMeta("RRDMaxi", ValueMeta.TYPE_STRING, 44, 0);
		v.setOrigin(origin);
		r.addValueMeta(v);
		*/
	}

	public Object clone()
	{
		Object retval = super.clone();
		return retval;
	}

	public void loadXML(Node stepnode, List<DatabaseMeta> databases, Map<String,Counter> counters)
		throws KettleXMLException
	{
		try
		{	
			setRRDFilename(XMLHandler.getTagValue(stepnode, "RRDFilename"));
			setRRDDatasource(XMLHandler.getTagValue(stepnode, "RRDDatasource"));
			setRRDType(XMLHandler.getTagValue(stepnode, "RRDType"));
			setRRDHeartBeat(XMLHandler.getTagValue(stepnode, "RRDHeartBeat"));
			setRRDStartTime(XMLHandler.getTagValue(stepnode, "RRDStartTime"));
			setRRDMini(XMLHandler.getTagValue(stepnode, "RRDMini"));
			setRRDMaxi(XMLHandler.getTagValue(stepnode, "RRDMaxi"));
			setRRDArchives(XMLHandler.getTagValue(stepnode, "RRDArchiveItems"));
		}
		catch(Exception e)
		{
			throw new KettleXMLException("Unable to read step info from XML node", e);
		}
	}

	public void setDefault()
	{
		RRDFilename = "";
		RRDDatasource = "";	
	}

	public void readRep(Repository rep, long id_step, List<DatabaseMeta> databases, Map<String,Counter> counters) throws KettleException
	{
		try
		{
			setRRDFilename(rep.getStepAttributeString(id_step,0, "RRDFilename"));
			setRRDDatasource(rep.getStepAttributeString(id_step, "RRDDatasource"));
			setRRDType(rep.getStepAttributeString(id_step, "RRDType"));
			setRRDHeartBeat(rep.getStepAttributeString(id_step, "RRDHeartBeat"));
			setRRDStartTime(rep.getStepAttributeString(id_step, "RRDStartTime"));
			setRRDMini(rep.getStepAttributeString(id_step, "RRDMini"));
			setRRDMaxi(rep.getStepAttributeString(id_step, "RRDMaxi"));
			setRRDArchives(rep.getStepAttributeString(id_step, "RRDArchiveItems"));
		}
		catch(KettleDatabaseException dbe)
		{
			throw new KettleException("error reading step with id_step="+id_step+" from the repository", dbe);
		}
		catch(Exception e)
		{
			throw new KettleException("Unexpected error reading step with id_step="+id_step+" from the repository", e);
		}
	}
	
	public void saveRep(Repository rep, long id_transformation, long id_step) throws KettleException
	{
		try
		{
			rep.saveStepAttribute(id_transformation, id_step, "RRDFilename", getRRDFilename());
			rep.saveStepAttribute(id_transformation, id_step, "RRDDatasource", getRRDDatasource());
			rep.saveStepAttribute(id_transformation, id_step, "RRDType", getRRDType());
			rep.saveStepAttribute(id_transformation, id_step, "RRDHeartBeat", getRRDHeartBeat());
			rep.saveStepAttribute(id_transformation, id_step, "RRDStartTime", getRRDStartTime());
			rep.saveStepAttribute(id_transformation, id_step, "RRDMini", getRRDMini());
			rep.saveStepAttribute(id_transformation, id_step, "RRDMaxi", getRRDMaxi());
			rep.saveStepAttribute(id_transformation, id_step, "RRDArchiveItems", getRRDArchives());
		}
		catch(KettleDatabaseException dbe)
		{
			throw new KettleException("Unable to save step information to the repository, id_step="+id_step, dbe);
		}
	}

	public void check(List<CheckResultInterface> remarks, TransMeta transmeta, StepMeta stepMeta, RowMetaInterface prev, String input[], String output[], RowMetaInterface info)
	{
		CheckResult cr;
		if (prev==null || prev.size()==0)
		{
			cr = new CheckResult(CheckResult.TYPE_RESULT_WARNING, "Not receiving any fields from previous steps!", stepMeta);
			remarks.add(cr);
		}
		else
		{
			cr = new CheckResult(CheckResult.TYPE_RESULT_OK, "Step is connected to previous one, receiving "+prev.size()+" fields", stepMeta);
			remarks.add(cr);
		}
		
		// See if we have input streams leading to this step!
		if (input.length>0)
		{
			cr = new CheckResult(CheckResult.TYPE_RESULT_OK, "Step is receiving info from other steps.", stepMeta);
			remarks.add(cr);
		}
		else
		{
			cr = new CheckResult(CheckResult.TYPE_RESULT_ERROR, "No input received from other steps!", stepMeta);
			remarks.add(cr);
		}
	}
	
	public StepDialogInterface getDialog(Shell shell, StepMetaInterface meta, TransMeta transMeta, String name)
	{
		return new UpdateRRDPluginDialog(shell, meta, transMeta, name);
	}

	public StepInterface getStep(StepMeta stepMeta, StepDataInterface stepDataInterface, int cnr, TransMeta transMeta, Trans disp)
	{
		return new UpdateRRDPlugin(stepMeta, stepDataInterface, cnr, transMeta, disp);
	}

	public StepDataInterface getStepData()
	{
		return new UpdateRRDPluginData();
	}
}
